<?php
/**
 * User: Jerry
 * Date: 2017/5/24
 * Time: 11:08
 */

namespace SDXApi\Model;


use Think\Model;

/**
 * Class CommunityTemplateModel
 * @author Jerry
 * @package SDXApi\Model
 */
class CommunityTemplateModel extends Model
{
    /**
     * @var string
     */
    protected $tableName = 'community_template';

    /**
     * @author Jerry
     * @param int $id
     * @return mixed|array
     */
    public function findTemplatesByCommunityId($id = 0)
    {
        $where = array(
            'community_id'  =>  $id,
        );

        $field = 'id, sort, template_type, community_id';

        return $this->field($field)->where($where)->select();
    }

    /**
     * @author Jerry
     * @param $data
     * @return bool|string
     */
    public function insertTemplate($data)
    {
        try{
            if ($this->create($data)) {
                return $this->add();
            }

            return $this->getDbError();
        }catch (\ErrorException $errorException){
            E('数据插入错误');
        }
    }
}